package com.handtransaction.service.impl;

import com.handtransaction.dao.AdministratorDao;
import com.handtransaction.dao.CategoryDao;
import com.handtransaction.dao.UserDao;
import com.handtransaction.entity.Administrator;
import com.handtransaction.entity.Category;
import com.handtransaction.entity.User;
import com.handtransaction.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdministratorImpl implements AdministratorService {
    @Autowired
    private AdministratorDao adao;

    //管理员登录
    @Override
    public Administrator adminLogin(Integer aid) {
       return adao.ReadAdminibyId(aid);
    }
    //修改管理员信息
    @Override
    public int updateAdminInfo(Administrator administrator) {
        return adao.updateAdminInfo(administrator);
    }

    //查找普通管理员
    @Override
    public Administrator queryGeneralAdmin(String idOrName) {
        List<Administrator> adminList = new ArrayList<>();
        Administrator admin = null;
        try {
            //字符转整型，如转成功则是通过id查询
            Integer id = Integer.valueOf(idOrName);
            admin = adao.ReadAdminibyId(id);
        }catch (Exception e){
            //转整型失败，则是通过名称查询
            admin = adao.ReadAdminByName(idOrName);
        }finally {
            if(admin == null){
                return new Administrator();
            }
            if(admin.getApower().equals("root")){
                return new Administrator();
            }
            return admin;
        }
    }

    @Override
    public Administrator queryAdmin(Administrator administrator) {
        return adao.readAdminByAdmin(administrator);
    }

    //新增普通管理员
    @Override
    public int createAdmin(Administrator administrator) {
        return adao.createAdminByRootAdmin(administrator);
    }

    //查看所有普通管理员
    @Override
    public List<Administrator> showAllGeneralAdmin() {
        return adao.readAllGeneralAdmins();
    }

    @Override
    public int deleteAdmin(Integer aid) {
        return adao.deleteAdminById(aid);
    }
}

